package day9;

import java.text.DecimalFormat;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class Homework {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //demo1();
        demo2();
        //demo3();
    }
    public static void demo1() throws ExecutionException, InterruptedException {
        FutureTask<double[]> ft=new FutureTask<>(()->{
            double[] result=new double[2];
            result[0]=100;
            result[1]=100;
            for(int i=1;i<=10;i++){
                 result[1]/=2;
                 result[0]+=2*result[1];
            }
            result[1]/=2;
            return result;
        });
        new Thread(ft).start();
        double[] result=ft.get();//阻塞线程
        DecimalFormat df=new DecimalFormat("#.##");
        System.out.println("总米数:"+df.format(result[0]));
        System.out.println("反弹:"+df.format(result[1]));
    }
    public static void demo2(){
        new Demo2Thread("A").start();
        new Demo2Thread("B").start();
    }
    public static void demo3(){
        Climbing c=new Climbing(2000);
        ClimbThread a=new ClimbThread(c,300,"A");
        ClimbThread b=new ClimbThread(c,400,"B");
        ClimbThread c1=new ClimbThread(c,500,"C");
        a.start();
        b.start();
        c1.start();
    }
    static class Demo2Thread extends Thread{
        static int n;
        public Demo2Thread(String name) {
            super(name);
        }
        public void run() {
            long start=System.currentTimeMillis();
            String name=Thread.currentThread().getName();
            try {
                while (n<10) {
                    long end=System.currentTimeMillis();
                    if(end-start>=3000) break;
                    if ("A".equals(name)) {
                        Thread.sleep(10);
                        n++;
                    } else {
                        Thread.sleep(20);
                        n++;
                    }
                    System.out.println(name+"变量更新"+n);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
class Climbing{//爬山对象
    int sum;//总长
    //int speed;//speed速度
    boolean win;//胜者 初始为false
    public Climbing(int sum) {
        this.sum = sum;
    }
}
class ClimbThread extends Thread{
    Climbing c;
    int speed;
    public ClimbThread(Climbing c,int speed,String name) {
        super(name);
        this.speed=speed;
        this.c=c;
    }

    public void run() {
        int sum=0;
        String name=Thread.currentThread().getName();
        while (true){
            try {
                Thread.sleep(speed);
                if(c.win) break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            sum+=100;
            System.out.println(name+"已经爬了"+sum+"米,剩余"+(c.sum-sum)+"米");
            if(sum>=c.sum){
                System.out.println(name+"已经达到终点比赛结束");
                c.win=true; break;
            }
            if(c.win) break;

        }

    }
}